R is a programming language designed originally for performing statistical analyses. However, it’s potential has been increased and nowadays it can be used for general data analysis, creating atractive plots or even for creating websites! You can download R from the official repository: CRAN.
If you have used spreadsheets to calculate stuff, you have already programmed.
In R, you can assign values to variables using <-. You don’t have to type it everytime, just Alt+-!
variable <- 2342
variable
## [1] 2342
You can use R from the unix command line or download an Integrated Development Environment (IDE) such as RStudio.
RStudio overview. Source: http://www.sthda.com/english/wiki/r-basics-quick-and-easy
Rscripts. Write your code and run it whenever you want using Ctrl + Enter.The basic types of variables that can be represented in R are the following:
332, 0.234."Barcelona", "woman". A special type of character is the class factor.TRUE or FALSE.You can check the type of variable using class().
class() function.
var <- "text" # character
class(var)
## [1] "character"
var <- 3242 # numeric
class(var)
## [1] "numeric"
var <- TRUE # logical
class(var)
## [1] "logical"
| Operator | Description |
|---|---|
+ |
addition |
- |
subtraction |
* |
multiplication |
/ |
division |
^ or ** |
exponentiation |
3+4*5.2/2^2
## [1] 8.2
| Operator | Description |
|---|---|
> |
greater than |
>= |
greater than or equal to |
== |
exactly equal to |
!= |
not equal to |
# Works with numerics
3 > 2
## [1] TRUE
# And also with characters
"class"=="class"
## [1] TRUE
We can combine several data types into more complex structures. The different structures one can create in R are the following:
c() of values. The type of all values should be the same.vector <- c(0,1,2,3,4,6)
vector
## [1] 0 1 2 3 4 6
matrix(). You can check which arguments you can pass to the matrix function typing ?matrix or help(matrix)mat <- matrix(vector, nrow=2)
mat
## [,1] [,2] [,3]
## [1,] 0 2 4
## [2,] 1 3 6
vector <- c('a', 'b', 'c', 'd', 'e', 'f')
mat <- matrix(vector, ncol=2)
mat
## [,1] [,2]
## [1,] "a" "d"
## [2,] "b" "e"
## [3,] "c" "f"
patients <- data.frame(patientID=1:4,
gender=c("male", "female", "male", "female"),
age=c(23, 45, 55, 22),
dead=c(F, T, T, F))
patients
## patientID gender age dead
## 1 1 male 23 FALSE
## 2 2 female 45 TRUE
## 3 3 male 55 TRUE
## 4 4 female 22 FALSE
patients$age # to select one column
## [1] 23 45 55 22
listName[[1]].list <- list(name="donors",
patientList=patients)
list
## $name
## [1] "donors"
##
## $patientList
## patientID gender age dead
## 1 1 male 23 FALSE
## 2 2 female 45 TRUE
## 3 3 male 55 TRUE
## 4 4 female 22 FALSE
list[[1]]
## [1] "donors"
list[["name"]]
## [1] "donors"
There are many basic functions that are already loaded into R. For example, data.frame() is a function that generates data.frames and help() is a function that loads the manual for a specific R function.
You can create you own functions in R as follows:
sq <- function(x) {
square <- x*x
return(square)
}
sq(2)
## [1] 4
Functions are approppriate when you are copy&pasting operations many times, but changing tiny details. It might be more time consuming to create a function, but then you an run it with the parameters you want without any additional effort.
Many times, there are functions out there for the things you want to do. This functions are wrapped in packages and you can download them and use them freely!